package com.yuju.controller.accounts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.account.enums.AccountStatusEnum;
import com.yuju.account.enums.AccountTypeEnum;
import com.yuju.account.enums.BusinessTypeDetailEnum;
import com.yuju.account.enums.BusinessTypeEnum;
import com.yuju.account.enums.SettleAccountReturnCodeEnum;
import com.yuju.account.model.OrderAccountStatModel;
import com.yuju.model.account.AccountModel;
import com.yuju.order.enums.RentTypeEnum;
import com.yuju.util.Utils;
import com.yuju.account.service.IAccountService;
import com.yuju.common.Result;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.vo.account.AccountVO;

/**
 * 类名：SettleController  <br />
 *
 * 功能：结账页面账务接口
 *
 * @author : jiaokn <br />
 * @Date : 2017年12月5日 上午9:50:41  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("settle")
public class SettleController extends BaseController<AccountVO, AccountModel>  {

	@Autowired
	private IAccountService accountService;
	
	/**
	 * 功能：结账页面账务查询接口<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月19日 下午4:53:34 <br/>
	 */
	@RequestMapping("listByOrderIds")
	@ResponseBody
	public Result listByOrderIds(@RequestBody JSONObject obj){
		String orderIds = obj.getString("orderIds");
		String[] splitOrderIds = orderIds.split(",");
		List<String> orderIdsList = Arrays.asList(splitOrderIds);
		for (int i = 0; i < splitOrderIds.length; i++) {
			splitOrderIds[i] = splitOrderIds[i].replaceAll(" ", "");
		}
		List<AccountModel> list = accountService.listByOrderIds(orderIdsList);
		OrderAccountStatModel orderAccountStatModel = accountService.statOrderAccount(list);
		Result result = new Result();
		if(Utils.isNotNull(list) || Utils.isNotNull(orderAccountStatModel)){
			result.setRows(list);
			result.setData(orderAccountStatModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_LISTQUERY_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_LISTQUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_LISTQUERY_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_LISTQUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增挂账<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 下午5:27:32 <br/>
	 */
	@RequestMapping("saveGZ")
	@ResponseBody
	public Result saveGZ(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		accountVO.setCreateUserId(accountVO.getEmpId());
		
		Result result = new Result();
		String i = accountService.saveGZ(convert(accountVO));
		if(Utils.isNotNull(i)){
			result.setData(i);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_GZ_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_GZ_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_GZ_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_GZ_FAILURE.getDesc());
		}
		
		return result;
	}
	
	/**
	 * 功能：新增销售点<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 下午3:00:55 <br/>
	 */
	@RequestMapping("saveXSD")
	@ResponseBody
	public Result saveXSD(@RequestBody JSONObject obj){
		Result result = new Result();
		Map<String, Object> param = new HashMap<>();
		
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		accountVO.setCreateUserId(obj.getString("empId"));
		
		//获得商品账务信息
		JSONArray jsonArray = obj.getJSONArray("list");
		String jsonString = jsonArray.toJSONString();
		ArrayList<AccountModel> accounts = JSON.parseObject(jsonString, new TypeReference<ArrayList<AccountModel>>(){});
		
		param.put("accounts", accounts);
		param.put("accountModel", convert(accountVO));
		
		int i = accountService.saveXSD(param);
		
		if(i>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_XSD_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_XSD_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_XSD_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_XSD_FAILURE.getDesc());
		}
		return result;
	}
	
	
	/**
	 * 功能：冲调接口<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 下午1:52:08 <br/>
	 */
	@RequestMapping("saveCT")
	@ResponseBody
	public Result saveCT(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		//更新旧账务
		accountVO.setUpdateUserId(accountVO.getEmpId());
		accountVO.setGmtModified(new Date());
		int i = accountService.saveCT(convert(accountVO));
		if(i>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_CT_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_CT_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_CT_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_CT_FAILURE.getDesc());
		}
		return result;
	}
	
	
	/**
	 * 功能：收押金<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 上午9:53:44 <br/>
	 */
	@RequestMapping("saveYJ")
	@ResponseBody
	public Result saveYJ(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		accountVO.setAmount(1);
		accountVO.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountVO.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountVO.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountVO.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_YJ.getValue());
		accountVO.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_YJ.getName());
		accountVO.setBusinessDetail(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail())+BusinessTypeEnum.BUSINESS_TYPE_YJ.getName());
		accountVO.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail()));
		accountVO.setCreateUserId(accountVO.getEmpId());
		String id = null;
		if(accountVO.getRentType() == RentTypeEnum.SHORT.getValue()){
			id = accountService.saveOrderAccount(convert(accountVO));
		}else{
			id = accountService.saveGetId(convert(accountVO));
		}
			
		if(Utils.isNotNull(id)){
			result.setData(id);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_YJ_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_YJ_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_YJ_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_YJ_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增收款<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 上午10:25:17 <br/>
	 */
	@RequestMapping("saveSK")
	@ResponseBody
	public Result saveSK(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		accountVO.setAmount(1);
		accountVO.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountVO.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountVO.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountVO.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_SK.getValue());
		accountVO.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_SK.getName());
		accountVO.setBusinessDetail(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail())+BusinessTypeEnum.BUSINESS_TYPE_SK.getName());
		accountVO.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail()));
		accountVO.setCreateUserId(accountVO.getEmpId());
		String id = accountService.saveOrderAccount(convert(accountVO));
		if(Utils.isNotNull(id)){
			result.setData(id);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_SK_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_SK_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_SK_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_SK_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增退款<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 上午10:25:17 <br/>
	 */
	@RequestMapping("saveTK")
	@ResponseBody
	public Result saveTK(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		accountVO.setAmount(1);
		accountVO.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountVO.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountVO.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountVO.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_TK.getValue());
		accountVO.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_TK.getName());
		accountVO.setBusinessDetail(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail())+BusinessTypeEnum.BUSINESS_TYPE_TK.getName());
		accountVO.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail()));
		accountVO.setCreateUserId(accountVO.getEmpId());
		String id = accountService.saveOrderAccount(convert(accountVO));
		if(Utils.isNotNull(id)){
			result.setData(id);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_TK_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_TK_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_TK_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_TK_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增房费<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 上午10:25:17 <br/>
	 */
	@RequestMapping("saveFF")
	@ResponseBody
	public Result saveFF(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		accountVO.setAmount(1);
		accountVO.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountVO.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountVO.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
		accountVO.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_JSFF.getValue());
		accountVO.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_JSFF.getName());
		accountVO.setBusinessDetail(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail())+BusinessTypeEnum.BUSINESS_TYPE_JSFF.getName());
		accountVO.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail()));
		accountVO.setCreateUserId(accountVO.getEmpId());
		String id =  accountService.saveOrderAccount(convert(accountVO));
		if(Utils.isNotNull(id)){
			result.setData(id);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_FF_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_FF_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_FF_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_FF_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增免单<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 上午10:25:17 <br/>
	 */
	@RequestMapping("saveMD")
	@ResponseBody
	public Result saveMD(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		accountVO.setAmount(1);
		accountVO.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountVO.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountVO.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountVO.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_MD.getValue());
		accountVO.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_MD.getName());
		accountVO.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getValue());
		accountVO.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail()));
		accountVO.setBusinessDetail(BusinessTypeEnum.BUSINESS_TYPE_MD.getName());
		accountVO.setCreateUserId(accountVO.getEmpId());
		String id = accountService.saveOrderAccount(convert(accountVO));
		if(Utils.isNotNull(id)){
			result.setData(id);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_MD_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_MD_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_MD_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_MD_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增其他收款<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 上午10:25:17 <br/>
	 */
	@RequestMapping("saveQT")
	@ResponseBody
	public Result saveQT(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		accountVO.setAmount(1);
		accountVO.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountVO.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountVO.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountVO.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_QT.getValue());
		accountVO.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_QT.getName());
		accountVO.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getValue());
		accountVO.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail()));
		accountVO.setBusinessDetail("支票号:"+accountVO.getVoucherNo());
		accountVO.setCreateUserId(accountVO.getEmpId());
		String id = accountService.saveOrderAccount(convert(accountVO));
		if(Utils.isNotNull(id)){
			result.setData(id);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_QT_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_QT_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_QT_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_QT_FAILURE.getDesc());
		}
		return result;
	}
	
}
